package cn.usian.controller;

import cn.usian.pojo.User;
import cn.usian.service.UserService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    //autowired按照类型配置
    //resource按照名称配置
    @Autowired
    private UserService userService;

    //查询
    @GetMapping("all")
    public String findAll(Model model){
        List<User>userList=userService.findAll();
        model.addAttribute("list",userList);
        return "list";
    }
    //去添加
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }
    //添加
    @RequestMapping("addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:/all";
    }

    //删除
    @RequestMapping("delete")
    public String delete(Integer uid){
        userService.delete(uid);
        return "redirect:/all";
    }
    //查询id的成员信息
    @RequestMapping("findById")
    public String findById(Integer uid,Model model){
        User user=userService.findById(uid);
        model.addAttribute("user",user);
        return "update";
    }

    //修改
    @RequestMapping("updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/all";
    }
}
